// transform-less-v5-inline.js
// 用法: node transform-less-v5-inline.js <less-dir>

const fs = require('fs');
const path = require('path');

// 只用最终 CSS 值，避免再出现未定义变量
const antd5VariableMap = {
  // 主题/功能色
  '@blue-6': '#1677ff',
  '@color-primary-6': '#1677ff',
  '@primary-color': '#1677ff',
  '@success-color': '#52c41a',
  '@warning-color': '#faad14',
  '@error-color': '#ff4d4f',
  '@info-color': '#1677ff',

  // 文本色
  '@heading-color': 'rgba(0,0,0,0.88)',
  '@text-color': 'rgba(0,0,0,0.88)',
  '@text-color-secondary': 'rgba(0,0,0,0.45)',
  '@disabled-color': 'rgba(0,0,0,0.25)',

  // 背景
  '@component-background': '#ffffff',
  '@layout-body-background': '#f5f5f5',
  '@layout-header-background': '#001529',
  '@layout-footer-background': '#f5f5f5',

  // Layout 尺寸
  '@layout-header-height': '64px',
  '@layout-header-padding': '0 50px',
  '@layout-header-color': 'rgba(0,0,0,0.88)',
  '@layout-footer-padding': '24px 50px',
  '@layout-sider-background': '#ffffff',
  '@layout-trigger-height': '48px',
  '@layout-trigger-background': '#ffffff',
  '@layout-trigger-color': 'rgba(0,0,0,0.88)',
  '@layout-zero-trigger-width': '36px',
  '@layout-zero-trigger-height': '42px',
  '@layout-sider-width': '200px',

  // 边框
  '@border-color-base': '#d9d9d9',
  '@border-color-split': '#f0f0f0',
  '@border-width-base': '1px',
  '@border-radius-base': '6px',
  '@border-style-base': 'solid',

  // Menu / Sider
  '@menu-collapsed-width': '80px',

  // 字体
  '@font-size-base': '14px',
  '@font-size-lg': '16px',
  '@font-size-sm': '12px',
  '@line-height-base': '1.5715',

  // 断点
  '@screen-xs': '480px',
  '@screen-sm': '576px',
  '@screen-md': '768px',
  '@screen-md-min': '768px',
  '@screen-lg': '992px',
  '@screen-xl': '1200px',
  '@screen-xxl': '1600px',

  // 间距
  '@padding-lg': '24px',
  '@padding-md': '16px',
  '@padding-sm': '8px',
  '@padding-xs': '4px',

  // 阴影 & 动画
  '@box-shadow-base': '0 3px 6px rgba(0,0,0,0.16)',
  '@box-shadow-light': '0 1px 3px rgba(0,0,0,0.12)',
  '@ease-in-out': 'cubic-bezier(0.4, 0, 0.2, 1)',
  '@ease-out': 'cubic-bezier(0.0, 0, 0.2, 1)',
  '@ease-in': 'cubic-bezier(0.4, 0, 1, 1)',
};

// 安全转义给正则用
function escapeForRegExp(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

// 先替换 @{var}，再替换 @var
function transformLessContent(content, filePath) {
  // 1) 删除 antd v3 默认主题导入
  content = content.replace(/@import\s+['"]antd\/lib\/style\/themes\/default\.less['"];\s*/g, '');

  // 2) 变量名按长度降序，避免部分匹配
  const entries = Object.entries(antd5VariableMap).sort(
    ([a], [b]) => b.length - a.length
  );

  // 3) 替换插值形式：@{var}
  for (const [oldVar, value] of entries) {
    const nameNoAt = oldVar.slice(1); // 去掉 @
    const interpRe = new RegExp(`@\\{\\s*${escapeForRegExp(nameNoAt)}\\s*\\}`, 'g');
    if (interpRe.test(content)) {
      console.log(`Replacing @{${nameNoAt}} -> ${value} in ${filePath}`);
      content = content.replace(interpRe, value);
    }
  }

  // 4) 替换普通形式：@var
  for (const [oldVar, value] of entries) {
    const plainRe = new RegExp(`(?<!@)${escapeForRegExp(oldVar)}`, 'g');
    if (plainRe.test(content)) {
      console.log(`Replacing ${oldVar} -> ${value} in ${filePath}`);
      content = content.replace(plainRe, value);
    }
  }

  return content;
}

// 递归处理目录
function processDir(dir) {
  for (const file of fs.readdirSync(dir)) {
    const filePath = path.join(dir, file);
    const stat = fs.statSync(filePath);
    if (stat.isDirectory()) {
      processDir(filePath);
    } else if (stat.isFile() && /\.less$/i.test(file)) {
      const raw = fs.readFileSync(filePath, 'utf-8');
      const out = transformLessContent(raw, filePath);
      if (out !== raw) {
        fs.writeFileSync(filePath, out, 'utf-8');
      }
    }
  }
}

// 主程序
if (process.argv.length < 3) {
  console.error('Usage: node transform-less-v5-inline.js <less-dir>');
  process.exit(1);
}

const rootDir = path.resolve(process.argv[2]);
processDir(rootDir);
console.log('All Less files have been transformed for Antd v5 (in place).');
